package com.j.comet;

import org.comet4j.core.CometContext;
import org.comet4j.core.CometEngine;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet(urlPatterns = { "/cometecho" })
public class CometEchoSevelet extends HttpServlet {

	private static final String CHANNEL = "hello";

	private static CometContext cc = null;

	@Override
	public void init() throws ServletException {
		super.init();
		System.out.println("初始化CometContext。。。");
		cc = CometContext.getInstance();
		//注册应用的channel
		cc.registChannel(CHANNEL);
	}

	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		System.out.println("service。。。");

		String msg = request.getParameter("msg");
		System.out.print("param msg:" + msg);
		CometEngine engine = CometContext.getInstance().getEngine();
		engine.sendToAll(CHANNEL, msg);
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		this.doGet(request, response);
	}

}
